<?php
namespace Jykj\Quicklogin\Controller;

/***************************************************************
 *
 *  Copyright notice
 *
 *  (c) 2015 WHB <wanghongbin@ngoos.org>
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

/**
 * The Controller for  Weibo
 */
class WeiboController extends CommonController{
	
	/**
	 * 远程登录成功后回调
	 */
	public function callbackAction(){
		$request_uri  = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
        $redirect_uri = $this->get_pure_url($request_uri);
        $appId        = $this->settings['weibo']['appId'];
        $appSecret    = $this->settings['weibo']['appKey'];

        $WeiBoO = new \SaeTOAuthV2($appId,$appSecret);
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = $redirect_uri;
            $token = $WeiBoO->getAccessToken('code', $keys);
            if ($token) {  
                $_SESSION['token'] = $token;  
                setcookie('weibojs_'.$WeiBoO->client_id,http_build_query($token));
                $WeiBoC = new \saetclientv2($appId,$appSecret,$token['access_token']);
                $ms      = $WeiBoC->home_timeline();  
                $uid_get = $WeiBoC->get_uid();  
                $uid     = $uid_get['uid'];  
                $usrinfo = $WeiBoC->show_user_by_id($uid); //获取用户的信息  
                if (isset($usrinfo['id'])) {
                    $accesstoken = $token['access_token'];
                    $openid = $usrinfo['id'];
                    $nickname = $usrinfo['screen_name'];
                    $userInfo = $this->frontendUserRepository->findByOpenid($usrinfo['id'],'openidWeibo');
                    if($userInfo){ //判断是否已绑定，未绑定进行绑定
						$userInfo->setAccessTokenWeibo($accesstoken);
						$userInfo->setNickname($nickname);
						$this->frontendUserRepository->update($userInfo);
						$this->redirect('login',NULL,NULL,array('fuser'=>$userInfo,'nickname'=>$nickname));
					}else{
						$this->redirect('bind',NULL,NULL,array('access_token'=>$accesstoken,'openid'=>$openid,'nickname'=>$nickname));
					}
                }
            }else {  
                return new \TYPO3\CMS\Extbase\Error\Error('授权失败！',1340193849);
            }
        }else{
            $this->redirect('callback');
        }
        /*$token = array(
            "access_token" => "2.00o67JLG0LOH9846792a4afdfOWSNE",
            "remind_in"    => "157679999",
            "expires_in"   => 157679999,
            "uid"          => "5661583184" );*/
	}
	
}